﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _3D
{
    class Model3D : GameObject3D
    {
        public Vector3 position;
        KeyboardState key, lastKey;
        bool colidi;
        public float rotation;
        MouseState mouse;
        public float sensivity = 1.5f;
        Vector2 lastMousePosition, mousePosition;
        public Boolean direita;
        float speed = 1f;
        public Model3D(Model modelo, Vector3 position)
            : base(modelo)
        {
            this.position = position;

        }
        public override void Update(Camera camera)
        {
            mouse = Mouse.GetState();

            mousePosition = new Vector2((float)mouse.X / 800, (float)mouse.Y / 600);

            key = Keyboard.GetState();

            if ((mousePosition.X != lastMousePosition.X && mousePosition.X > lastMousePosition.X))
            {
                rotation += 0.02f;
                Mouse.SetPosition(0, mouse.Y);
                mouse = Mouse.GetState();
                mousePosition = RmousePosition(mouse);
                direita = true;
            }
            else if ((mousePosition.X != lastMousePosition.X && mousePosition.X <= lastMousePosition.X))
            {
                rotation -= 0.02f;
                Mouse.SetPosition(0, mouse.Y);
                mouse = Mouse.GetState();
                mousePosition = RmousePosition(mouse);
                direita = false;
            }

            if (key.IsKeyDown(Keys.Down))
            {
                    position.X -= (float)(Math.Sin(rotation)) * speed;
                    position.Z -= (float)(Math.Cos(rotation)) * speed;
                    lastKey = key;
            }
            if (key.IsKeyDown(Keys.Up))
            {
                    position.X += (float)(Math.Sin(rotation)) * speed;
                    position.Z += (float)(Math.Cos(rotation)) * speed;
                    lastKey = key;
                
            }
            if (key.IsKeyDown(Keys.J))
            {
                position = new Vector3(position.X, position.Y + 0.2f, position.Z);
            }
            if (key.IsKeyDown(Keys.K))
            {
                position = new Vector3(position.X, position.Y - 0.2f, position.Z);
            }

            lastMousePosition = mousePosition;

            world = Matrix.Identity;

            world *= Matrix.CreateRotationY(rotation);
            world *= Matrix.CreateTranslation(position);
            base.Update(camera);
        }
        public Vector2 RmousePosition(MouseState mouse)
        {
            return new Vector2((float)mouse.X / 800, (float)mouse.Y / 600);
        }
    }
}
